import time
import json
import check_passwd

# 初始化用
name_user = flag = ""
# user_dick = {" ": " "}
# 用于格式化
tplt = "{0:{4}^10}\t{1:{4}^10}\t{2:{4}^10}\t{3:{4}^10}"


# 读取用户列表
def read():
    # global user_dick
    f = open('sysuserinfo.txt', 'r', encoding='utf-8')
    user_dick = json.load(f)
    f.close()
    return user_dick


# 修改用户列表
def wirt(user_dick):
    # global user_dick
    f = open('sysuserinfo.txt', 'w', encoding='utf-8')
    json.dump(user_dick, f)
    f.close()
    return user_dick


# 用户登录
def login():
    global flag, name_user
    uer_dick = read()
    while True:
        for i in range(3):
            name = input("输入用户名:")
            if name in uer_dick.keys():
                passwd = input("请输入密码：")
                if passwd == "":
                    print("密码不能为空！".center(50))
                elif passwd == "quit":
                    print("即将退出...".center(50))
                    time.sleep(3)
                    print("欢迎下次使用！".center(50))
                    print("-".rjust(50, '-'))
                    return
                elif passwd == uer_dick.get(name)[0]:
                    name_user = name
                    print("登录成功！".center(50))
                    print(f"欢迎\033[36;1m {name} \033[0m!".center(60))
                    print("-".rjust(50, '-'))
                    flag = uer_dick.get(name)[2]
                    return
                else:
                    print("密码错误！".center(50))
            elif name == "":
                print("用户名不能为空！".center(50))
            elif name == "quit":
                print("即将退出...".center(50))
                time.sleep(3)
                print("欢迎下次使用！".center(50))
                print("-".rjust(50, '-'))
                return
            else:
                print("用户名错误！".center(50))
        print("\033[31;7m已错误三次！请五秒后再尝试！\033[0m".center(50))
        time.sleep(5)


# 用户注册模块
def logon():
    user_dick = read()
    while 1:
        new_name = input("请输入新用户的名字：")
        if new_name in user_dick.keys():
            print("该用户名已被使用！".center(50))
        elif new_name == "":
            print("用户名不能为空！".center(50))
        else:
            break
    print("密码需符合密码复杂度要求！".center(50))
    while 1:
        new_passwd = input("请输入新用户的密码：")
        tem_list = check_passwd.check_passwd(passwd=new_passwd)
        if new_passwd == "":
            print("密码不能为空！".center(50))
        elif tem_list[0] < 4:
            print(f"密码级别为：{tem_list[0]},{tem_list[1:]}".center(50))
        else:
            break
    new_hobby = input("请输入新用户的爱好：")
    user_dick[new_name] = [new_passwd, new_hobby, '0']
    wirt(user_dick)
    print(f"{new_name}用户添加完毕！".center(50))
    print(f"新用户{new_name}信息如下：".center(50))
    print("*".rjust(50, '*'))
    print(tplt.format("用户", "密码", "爱好", "权限", " "))
    print(tplt.format(new_name,
                      user_dick.get(new_name)[0],
                      user_dick.get(new_name)[1],
                      user_dick.get(new_name)[2], " "))
    print("*".rjust(50, '*'))
    input("按回车返回主菜单")
    main()


# 一级菜单
def menu_f():
    print("\n",
          "1.登录".center(50), "\n",
          "2.注册".center(50), "\n",
          "3.退出".center(50), "\n",
          "-".rjust(50, '-'))
    while 1:
        first_choice = input("请选择：")
        if first_choice == "1":
            print("#".rjust(50, "#"))
            login()
            break
        elif first_choice == "2":
            print("#".rjust(50, "#"))
            logon()
            break
        elif first_choice == "3":
            print("欢迎下次使用！".center(50))
            print("-".rjust(50, '-'))
            return
        else:
            print("请输入正确选项！")
            print("-".rjust(50, "-"))


# 管理员菜单
def menu_s_adm():
    while 1:
        print("1.查看所有用户信息".center(50), "\n",
              "2.修改用户信息".center(50), "\n",
              "3.删除用户".center(50), "\n",
              "4.添加用户".center(50), "\n",
              "5.返回上一级菜单".center(50), "\n",
              "6.退出".center(50), "\n",
              "-".rjust(50, '-'))
        adm_choice = input("请选择：")
        if adm_choice == "1":
            check()
            input("按回车继续")
            print("-".rjust(50, '-'))
        elif adm_choice == "2":
            modify()
            input("按回车继续")
            print("-".rjust(50, '-'))
        elif adm_choice == "3":
            deluser()
            input("按回车继续")
            print("-".rjust(50, '-'))
        elif adm_choice == "4":
            add()
            input("按回车继续")
            print("-".rjust(50, '-'))
        elif adm_choice == "5":
            main()
            print("-".rjust(50, '-'))
            return
        elif adm_choice == "6":
            print("即将退出...".center(50))
            time.sleep(3)
            print("欢迎下次使用！".center(50))
            print("-".rjust(50, '-'))
            return


# 用户菜单
def menu_s_com():
    global name_user
    user_dick = read()
    while 1:
        print("1.修改当前爱好".center(50), "\n",
              "2.返回上一级菜单".center(50), "\n",
              "3.退出".center(50))
        print("-".rjust(50, '-'))
        adm_choice = input("请选择：")
        if adm_choice == "1":
            new_hobby = input("请输入新爱好：")
            user_dick.get(name_user)[2] = new_hobby
            f = open('test.txt', 'w', encoding='utf-8')
            json.dump(user_dick, f)
            f.close()
            print("爱好已成功修改！")
            print("-".rjust(50, '-'))
            break
        elif adm_choice == "2":
            main()
            print("-".rjust(50, '-'))
            return
        elif adm_choice == "3":
            print("即将退出...".center(50))
            time.sleep(3)
            print("欢迎下次使用！".center(50))
            print("-".rjust(50, '-'))
            return


# 查看所有用户信息
def check():
    user_dick = read()
    print("*".rjust(50, '*'))
    print(tplt.format("用户", "密码", "爱好", "权限", " "))
    for i, j in user_dick.items():
        print(tplt.format(i, j[0], j[1], j[2], " "))
    print("*".rjust(50, '*'))


# 修改用户信息
def modify():
    user_dick = read()
    name = input("请输入要修改的用户：")
    while name not in user_dick.keys():
        print("用户不存在")
        name = input("请输入要修改的用户：")
    while 1:
        new_name = input("请输入新用户的名字：")
        # 除去自己，即可不改名
        user_dick.pop(name)
        if new_name in user_dick.keys():
            print("该用户名已被使用！")
        elif new_name == "":
            print("用户名不能为空！")
        else:
            break
    print("密码需符合密码复杂度要求！".center(50))
    while 1:
        new_passwd = input("请输入新用户的密码：")
        tem_list = check_passwd.check_passwd(passwd=new_passwd)
        if new_passwd == "":
            print("密码不能为空！".center(50))
        elif tem_list[0] < 4:
            print(f"密码级别为：{tem_list[0]},{tem_list[1:]}".center(50))
        else:
            break
    new_hobby = input("请输入新爱好：")
    new_lim = input("请输入新权限等级(0/1)(普通用户/管理员)：")
    user_dick[new_name] = [new_passwd, new_hobby, new_lim]
    wirt(user_dick)
    print(f"{name}的信息修改完毕！".center(50))
    print("新信息如下：".center(50))
    read()
    print("*".rjust(50, '*'))
    print(tplt.format("用户", "密码", "爱好", "权限", " "))
    print(tplt.format(new_name,
                      user_dick.get(new_name)[0],
                      user_dick.get(new_name)[1],
                      user_dick.get(new_name)[2], " "))
    print("*".rjust(50, '*'))


# 删除用户
def deluser():
    user_dick = read()
    name = input("请输入需要删除的用户：")
    user_dick.pop(name)
    wirt(user_dick)
    print(f"用户{name}已删除")
    print("-".rjust(50, '-'))


# 添加新用户
def add():
    user_dick = read()
    while 1:
        new_name = input("请输入新用户的名字：")
        if new_name in user_dick.keys():
            print("该用户名已被使用！")
        elif new_name == "":
            print("用户名不能为空！")
        else:
            break
    print("密码需符合密码复杂度要求！".center(50))
    while 1:
        new_passwd = input("请输入新用户的密码：")
        tem_list = check_passwd.check_passwd(passwd=new_passwd)
        if new_passwd == "":
            print("密码不能为空！".center(50))
        elif tem_list[0] < 4:
            print(f"密码级别为：{tem_list[0]},{tem_list[1:]}".center(50))
        else:
            break
    new_hobby = input("请输入新用户的爱好：")
    new_lim = input("请输入新用户的权限等级(0/1)(普通用户/管理员)：")
    user_dick[new_name] = [new_passwd, new_hobby, new_lim]
    wirt(user_dick)
    print(f"{new_name}用户添加完毕！".center(50))
    print(f"新用户{new_name}信息如下：".center(50))
    print("*".rjust(50, '*'))
    print(tplt.format("用户", "密码", "爱好", "权限", " "))
    print(tplt.format(new_name,
                      user_dick.get(new_name)[0],
                      user_dick.get(new_name)[1],
                      user_dick.get(new_name)[2], " "))
    print("*".rjust(50, '*'))


# 主函数
def main():
    menu_f()
    if flag == "1":
        menu_s_adm()
    elif flag == "0":
        menu_s_com()


# 调用主函数,启动程序
print("欢迎使用用户管理系统！".center(50))
main()
